<template>
  <div>
    <div class="search-panel">
      <el-form :inline="true" label-width="50">
        <el-form-item label="最新">
          <el-radio-group v-model="radioData" @change="changeRadio()">
            <el-radio :label="1">当天</el-radio>
            <el-radio :label="2">3天</el-radio>
            <el-radio :label="3">7天</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="time"
            type="datetimerange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            :default-time="['00:00:00','23:59:59']"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="行业类别">
          <el-select v-model="query.industryCategory" placeholder="请选择">
            <el-option
              v-for="item in industryCategorys"
              :key="item.code"
              :value="item.code"
              :label="item.codeName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="控制级别">
          <el-select v-model="query.controlLevel" placeholder="请选择" clearable>
            <el-option
              v-for="item in controlLevels"
              :key="item.code"
              :value="item.code"
              :label="item.codeName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="排放类型">
          <el-select v-model="query.emissionType" placeholder="请选择" clearable>
            <el-option
              v-for="item in emissionTypes"
              :key="item.code"
              :value="item.code"
              :label="item.codeName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报警类型">
          <el-select v-model="query.alarmType" placeholder="请选择" clearable>
            <el-option
              v-for="item in alarmTypes"
              :key="item.code"
              :value="item.code"
              :label="item.codeName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="处理状态">
          <el-select v-model="query.processingStatus" placeholder="请选择" clearable>
            <el-option
              v-for="item in processingStatuss"
              :key="item.code"
              :value="item.code"
              :label="item.codeName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否解除">
          <el-select v-model="query.remove" placeholder="请选择" clearable>
            <el-option
              v-for="item in removes"
              :key="item.code"
              :value="item.code"
              :label="item.codeName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否需要处理">
          <el-select v-model="query.process" placeholder="请选择" clearable>
            <el-option
              v-for="item in processs"
              :key="item.code"
              :value="item.code"
              :label="item.codeName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报警时长持续">
          <el-input v-model="query.duration" width="8" />
        </el-form-item>
        <el-form-item label="小时以上" />
        <el-button type="primary" @click="initTable">查询</el-button>
      </el-form>
    </div>
    <div class="table-panel">
      <el-table
        ref="table"
        v-loading="tableLoading"
        class="grid-data-table"
        height="400px"
        :data="tableData"
        @selection-change="handleSelectionChange"
      >
        <el-table-column align="center" type="selection" width="80" />
        <el-table-column prop="alarmInformationObj.enterpriseName" label="企业名称" />
        <el-table-column prop="alarmInformationObj.outletName" label="排口名称" />
        <el-table-column prop="alarmType" label="报警类型">
          <template slot-scope="scope">
            {{ getDictLabel('e001', scope.row.alarmType) }}
          </template>
        </el-table-column>
        <el-table-column prop="alarmInformation" label="报警信息" width="200" />
        <el-table-column prop="alarmStartTime" label="报警开始时间">
          <template slot-scope="scope">
            <div>{{ parseTime(scope.row.alarmStartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="alarmEndTime" label="报警结束时间">
          <template slot-scope="scope">
            <div>{{ parseTime(scope.row.alarmEndTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="duration" label="持续时长">
          <template v-if="scope.row.duration" slot-scope="scope">
            {{ scope.row.durationStr }} ( {{ scope.row.duration }} )
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="60">
          <template slot-scope="scope">
            <div>
              <el-button type="text" size="small" @click="handleQueryYz(scope.row)">因子</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <Pagination
          v-show="query.total>0"
          :total="query.total"
          :page.sync="query.page"
          :limit.sync="query.limit"
          @pagination="initTable"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { parseTime } from '@/utils/index'
import { getList } from '@/api/business/alarm'
import { getDict } from '@/utils/dict'
import { getDictLabel } from '@/utils'
import { getDatefmt } from '@/utils/common'
import Pagination from '@/components/Pagination'
export default {
  components: {
    Pagination
  },
  props: {
    outletIds: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      radioData: 1,
      time: [],
      tableLoading: false,
      query: {
        total: 0,
        page: 1,
        limit: 10,
        controlLevel: '',
        industryCategory: '',
        emissionType: '',
        alarmType: '',
        processingStatus: '',
        remove: '',
        process: '',
        duration: null,
        projectDepartmentId: null,
        enterpriseId: null,
        outletIds: null
      },
      resetState1: true,
      resetState2: true,
      tableData: [],
      controlLevels: [],
      industryCategorys: [],
      emissionTypes: [],
      alarmTypes: [],
      processingStatuss: [],
      removes: [],
      processs: []
    }
  },
  watch: {
  },
  created() {
    this.industryCategorys = getDict('e013')
    this.emissionTypes = getDict('e012')
    this.controlLevels = getDict('e014')
    this.alarmTypes = getDict('e001')
    this.processingStatuss = getDict('e003')
    this.removes = getDict('e002')
    this.processs = getDict('e004')
    this.changeRadio()
  },
  methods: {
    parseTime, getDict, getDictLabel,
    handleQueryYz(row) {
      let times = null
      if (row.alarmStartTime && row.alarmEndTime) {
        times = [
          parseTime(row.alarmStartTime, '{y}-{m}-{d} {h}:{i}:{s}'),
          parseTime(row.alarmEndTime, '{y}-{m}-{d} {h}:{i}:{s}')
        ]
      }
      this.$emit('queryOther', row, times)
    },
    initTable() {
      this.query.outletIds = this.outletIds
      if (this.time !== '' && this.time !== null) {
        this.query.alarmStartTime = this.time[0]
        this.query.alarmEndTime = this.time[1]
      } else {
        this.query.alarmStartTime = null
        this.query.alarmEndTime = null
      }
      this.tableLoading = true
      getList(this.query).then(res => {
        this.tableData = res.data.rows
        this.query.total = res.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    changeRadio() {
      this.time = []
      const date = new Date()
      this.$set(this.time, 1, getDatefmt(date, 'yyyy-MM-dd') + ' 23:59:59')
      if (this.radioData === 1) {
        this.$set(this.time, 0, getDatefmt(new Date(), 'yyyy-MM-dd') + ' 00:00:00')
      } else if (this.radioData === 2) {
        this.$set(this.time, 0, getDatefmt(new Date(new Date().setDate(date.getDate() - 3)), 'yyyy-MM-dd') + ' 00:00:00')
      } else {
        this.$set(this.time, 0, getDatefmt(new Date(new Date().setDate(date.getDate() - 7)), 'yyyy-MM-dd') + ' 00:00:00')
      }
    },
    handleSelectionChange(val) {
      let str = ''
      let state = true
      if (val.length > 0) {
        str = val[0].processingStatus
      }
      val.forEach(item => {
        if (item.processingStatus === str) {
          state = false
        } else {
          state = true
          return
        }
      })
      if (str === 'e003_01') {
        this.resetState1 = state
      } else if (str === 'e003_02') {
        this.resetState2 = state
      } else if (str === '') {
        this.resetState1 = state
        this.resetState2 = state
      }
    },
    handleDetails() {

    }
  }
}
</script>

<style lang="scss" scoped>

</style>
